package com.example.newland.application92;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private TextView main_tv_temp;
    private TextView main_tv_hum;
    private SharedPreferences config;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_tv_temp = findViewById(R.id.main_tv_temp);
        main_tv_hum = findViewById(R.id.main_tv_hum);

        new Thread(){

            @Override
            public void run() {
                super.run();
                //请求网络，获取token
                String login_url = "http://api.nlecloud.com/Users/Login";
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("Account","17309339946");
                    jsonObject.put("Password","17309339946");
                    jsonObject.put("IsRememberMe",true);
                    String pamer = jsonObject.toString();
                    String login_json = HttpUrl.post(login_url, pamer, null);
                    Log.i("登录json",login_json);

                    JSONObject jsonObject1 = new JSONObject(login_json);
                    JSONObject resultObj = jsonObject1.getJSONObject("ResultObj");
                    String accessToken = resultObj.getString("AccessToken");

                    config = getSharedPreferences("config", MODE_PRIVATE);
                    SharedPreferences.Editor edit = config.edit();
                    edit.putString(Config.token,accessToken);
                    edit.commit();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //子线程请求温度
                SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                String token = config.getString(Config.token, "");
                while(true){
                    String temp_url ="http://api.nlecloud.com/devices/778003/Datas?deviceId=778003&ApiTags=m_temp";
                    String temp_json = HttpUrl.get(temp_url, token);
                    Log.i("温度json",temp_json);

                    try {
                        JSONObject jsonObject = new JSONObject(temp_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String value = pointDTOJSONObject.getString("Value");
                        Log.e("温度",value);

                        Message message = new Message();
                        message.what = 1;

                        Bundle bundle = new Bundle();
                        bundle.putString("value",value);
                        message.setData(bundle);

                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        Log.e("err",e.getMessage().toString());
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //子线程请求温度
                SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                String token = config.getString(Config.token, "");
                while(true){
                    String hum_url ="http://api.nlecloud.com/devices/778003/Datas?deviceId=778003&ApiTags=m_hum";
                    String hum_json = HttpUrl.get(hum_url, token);
                    Log.i("湿度json",hum_json);

                    try {
                        JSONObject jsonObject = new JSONObject(hum_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String value = pointDTOJSONObject.getString("Value");
                        Log.e("湿度",value);

                        Message message = new Message();
                        message.what = 2;

                        Bundle bundle = new Bundle();
                        bundle.putString("value",value);
                        message.setData(bundle);

                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        Log.e("err",e.getMessage().toString());
                    }
                }
            }
        }.start();

    }

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 1:
                    Bundle data = msg.getData();
                    String value = data.getString("value");
                    main_tv_temp.setText(value);
                    break;
                case 2:
                    Bundle data1 = msg.getData();
                    String value1 = data1.getString("value");
                    main_tv_hum.setText(value1);
                    break;
            }
        }
    };
}
